﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace Empires.Infrastructure.Entities.Repository
{
    [Table("T_Corporation")]
    public class T_Corporation : IEntity<T_Corporation>
    {
        [Column(TypeName="varchar")]
        [StringLength(128)]
        [Key]
        public string Master { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string SiteName { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(256)]
        public string Logo { get; set; }
        
        [Column(TypeName = "nvarchar")]
        [StringLength(512)]
        public string Description { get; set; }

        [Column(TypeName = "int")]
        public int Area { get; set; }

        [Column(TypeName = "int")]
        public int Location { get; set; }

        /// <summary>
        /// 所属行业
        /// </summary>
        public int Industry { get; set; }

        /// <summary>
        /// 企业名，通常站点名和企业名相同
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string CorporationName { get; set; }
        /// <summary>
        /// 公司联系电话
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string CopPhone { get; set; }
        /// <summary>
        /// 公司地址
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string CopAddress { get; set; }
        /// <summary>
        /// 公司联系人
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string CopContact { get; set; }
        /// <summary>
        /// ICP号
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(64)]
        public string ICP { get; set; }
        public int TempId { get; set; }
        /// <summary>
        /// 缩写
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(64)]
        public string ShortName { get; set; }

        public override T_Corporation Find(string key)
        {
            return Find(c => c.Master == key);
        }

        public override T_Corporation Find(System.Linq.Expressions.Expression<Func<T_Corporation, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Corporation.FirstOrDefault(where);
                if (item != null)
                {
                    this.Area = item.Area;
                    this.Location = item.Location;
                    this.Description = item.Description;
                    this.Industry = item.Industry;
                    this.Logo = item.Logo;
                    this.Master = item.Master;
                    this.SiteName = item.SiteName;
                    this.CorporationName = item.CorporationName;
                    this.CopAddress = item.CopAddress;
                    this.CopContact = item.CopContact;
                    this.CopPhone = item.CopPhone;
                    this.ICP = item.ICP;
                    this.TempId = item.TempId;
                    this.ShortName = item.ShortName;
                    return this;
                }
                return null;
            }
        }
        public override bool Any(System.Linq.Expressions.Expression<Func<T_Corporation, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Corporation.Any(where);
            }
        }
    }
}
